<template>
  <div>
    <Dialog
      :show="dialogConfig.show"
      :title="dialogConfig.title"
      :buttons="dialogConfig.buttons"
      width="500px"
      :showCancel="true"
      @close="dialogConfig.show = false"
    >
      <el-form
        :model="formData"
        :rules="rules"
        ref="formDataRef"
        label-width="80px"
        @submit.prevent
      >
        <!--input输入-->
        <el-form-item label="新密码" prop="password">
          <el-input
            type="password"
            size="large"
            placeholder="请输入密码"
            v-model="formData.password"
            show-password
          >
            <template #prefix>
              <span class="iconfont icon-password"></span>
            </template>
          </el-input>
        </el-form-item>
        <!--input输入-->
        <el-form-item label="确认密码" prop="rePassword">
          <el-input
            type="password"
            size="large"
            placeholder="请再次输入密码"
            v-model="formData.rePassword"
            show-password
          >
            <template #prefix>
              <span class="iconfont icon-password"></span>
            </template>
          </el-input>
        </el-form-item>
      </el-form>
    </Dialog>
  </div>
</template>

<script setup>
import { ref, reactive, getCurrentInstance, nextTick, inject, onMounted, watch } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import http from '@/utils/http';
const { proxy } = getCurrentInstance();
const router = useRouter();
const route = useRoute();
// 注入
const userId=inject('userID');
const $message=inject('$message');


// 确保 formData 在使用之前被定义
const formData = reactive({
  userId: null,
  password: "",
  rePassword: ""
});


// 表单引用
const formDataRef = ref(null);

// 验证规则
const checkRePassword = (rule, value, callback) => {
  if (value !== formData.rePassword) {
    callback(new Error(rule.message));
  } else {
    callback();
  }
};

const rules = {
  password: [
    { required: true, message: "请输入密码" },
    {
      validator: proxy.Verify.password,
      message: "密码只能是数字，字母，特殊字符 8-18位",
    },
  ],
  rePassword: [
    { required: true, message: "请再次输入密码" },
    {
      validator: checkRePassword,
      message: "两次输入的密码不一致",
    },
  ],
};

// 显示对话框的方法
const show = () => {
  dialogConfig.value.show = true;
  nextTick(() => {
    formDataRef.value.resetFields();
    Object.assign(formData, { userId: null, password: "", rePassword: "" });
  });
};

defineExpose({ show });

const dialogConfig = ref({
  show: false,
  title: "修改密码",
  buttons: [
    {
      type: "primary",
      text: "确定",
      click: (e) => {
        submitForm();
      },
    },
  ],
});

// 提交表单的方法


const submitForm = async () => {
  formDataRef.value.validate(async (valid) => {
    if (!valid) {
      return;
    }
    formData.userId = userId;
    const data=http.put('/user/update', formData);
    data.then((res) => {
      if (res.code === 200) {
        dialogConfig.value.show = false;
        router.push('/');
        $message.success('修改密码成功,请重新登录');
        
      } else {
        console.log(res.message);
      }
    });
  });
};
</script>

<style lang="scss">
</style>